﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.Drawing;
using Vibz.Common.Model;

namespace Vibz.Game.Bugs.Model
{
    [Serializable]
    [XmlInclude(typeof(Power))]
    [XmlInclude(typeof(Player))]
    [XmlInclude(typeof(Obstacle))]
    [XmlInclude(typeof(Compartment))]
    [XmlInclude(typeof(Coin))]
    public abstract class GroundObject : ModelObject
    {
        [XmlElement("ic")]
        public bool IsConsumed { get { return Owner.GetType() != typeof(Ground); } }

        [XmlElement("t")]
        public int Thickness = 20;

        Point _location;
        [XmlElement("l")]
        public Point Location
        {
            get
            {
                if (_location == Point.Empty && Owner != null)
                {
                    Random rnd = new Random();
                    _location = new Point()
                    {
                        X = rnd.Next(1, ((Ground)Owner).Width),
                        Y = rnd.Next(1, ((Ground)Owner).Height)
                        };
                }
                return _location;
            }
            set
            {
                _location = value;
                // Should not be notified
                //NotifyValueChanged("Location", value);
            }
        }

        [XmlIgnore()]
        public Point Center
        {
            get
            {
                return new Point(Location.X + (Thickness / 2), Location.Y + (Thickness / 2));
            }
        }

        public GroundObject() { }
        public GroundObject(Ground gnd) { Owner = gnd; }
        public virtual void Merge(Player player)
        {
            this.Owner = player;
            MergeOnPlayer(player);
        }
        public abstract void MergeOnPlayer(Player player);
        public override bool Equals(object obj)
        {
            return (this.ID == ((GroundObject)obj).ID);
        }
        public override int GetHashCode()
        {
            return base.GetHashCode();
        }
        public virtual void Add(GroundObject obj) { }
        public override string ToString()
        {
            return this.GetType().Name + ":" + this.ID.ToString();
        }
    }
}
